package com.example.kpopnews.theme;

import java.util.Stack;

import com.example.kpopnews.R;
import com.example.kpopnews.utils.Utils;

import android.app.Activity;
import android.content.Context;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

//******************************************************************
public final class Theme
// ******************************************************************
{
    // ******************************************************************
    public static void applyTheme(Activity activity)
    // ******************************************************************
    {
        View viewParent = activity.getWindow().getDecorView();

        Stack<View> stackViews = new Stack<View>();
        stackViews.push(viewParent);

        while (!stackViews.isEmpty())
        {
            View view = stackViews.pop();
            if (view instanceof ViewGroup)
            {
                ViewGroup viewGroup = (ViewGroup) view;
                for (int indexView = 0; indexView < viewGroup.getChildCount(); indexView++)
                {
                    view = viewGroup.getChildAt(indexView);
                    if (view instanceof ViewGroup)
                    {
                        stackViews.push(view);
                    }
                    else
                    {
                        applyTheme(activity, view);
                    }
                }
            }
            else
            {
                applyTheme(activity, view);
            }
        }
    }

    // ******************************************************************
    private static void applyTheme(Context context, View view)
    // ******************************************************************
    {
    	if(view.getContentDescription() == null)
    	{
    		return;
    	}
    	
        String contentDescriptions = view.getContentDescription().toString();
        if (view instanceof TextView)
        {
            TextView textView = (TextView) view;
            if(contentDescriptions.equalsIgnoreCase(context.getString(R.string.text_small)))
            {
                textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, Utils.getSmallSize());
            }
            else if(contentDescriptions.equalsIgnoreCase(context.getString(R.string.text_medium)))
            {
                textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, Utils.getMediumSize());
            }
            else if(contentDescriptions.equalsIgnoreCase(context.getString(R.string.text_large)))
            {
                textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, Utils.getLargeSize());
            } 
        }
        
        if(contentDescriptions.equalsIgnoreCase("..."))
        {
            // TODO apply images
        }
    }
}
